<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>

    //#####核心：计算出每个柱子能接多少水 计算方式 左边最高 和右边最高比较出最小的减去自身的高度就是自身能乘的水：Math.min(l_max,r_max)-height[i]
   // let height = [0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1]
    let height = [4, 2, 0, 3, 2, 5]
    var trap = function (height) {
        if (height.length === 0) return 0
        let n = height.length
        let res = 0
        let left = 0
        let right = n - 1
        let l_max = height[0]
        let r_max = height[n - 1]
        while (left <= right) {
            l_max = Math.max(height[left], l_max)
            r_max = Math.max(height[right], r_max)
            if (l_max < r_max) {
                res += l_max - height[left]
                left++
            } else {
                res += r_max - height[right]
                right--
            }
        }
        console.log(res, "=====s");

        return res
    };
    trap(height)
</script>

</html>